<!DOCTYPE html>
<html>
   <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <link rel="StyleSheet" type="text/css" href="/tpa/admin.css" />
      <link rel="StyleSheet" type="text/css" href="/tpa/jquery-ui.css" />

      <script type="text/javascript" src="/lib/js/jquery.js"></script>
      <script type="text/javascript" src="/lib/js/jquery.ui.js"></script>
      <script type="text/javascript" src="/lib/js/jquery.validate.js"></script>
      <script type="text/javascript" src="/lib/js/handlebars.js"></script>
      <script type="text/javascript" src="/lib/js/meccano.js"></script>
      <script type="text/javascript" src="/lib/js/jquery.maskedinput.js"></script>

      <script type="text/javascript">
         var $tabs;
         var $askTemplate;
         var tpl_templates;
         $(document).ready(function(){
            $tabs = $('#tabedContent').tabs({
               add: function(event, ui) {
                  $tabs.tabs('select', '#' + ui.panel.id);
               }, 
               load: function(event, ui) {
                  $("#" + ui.panel.id + " .date").mask("99.99.9999 99:99");
               }
            });

            $("#templateArea").validate({
               rules: {
                  'f[title]': 'required',
                  'f[template]': 'required'
               },
               errorPlacement: function(){},
               submitHandler: function(form)
               {
                  $.post(
                     '/admin/dispatch.php?a=1',
                     $("#templateArea").serialize(),
                     afterTemplSaved,
                     'json'
                  );
               }
            });

            $("#newdispatchtemplate").click(function(){
               $("#templateArea").find('textarea').val('').end().find('input[type=text]').val('').end().parent().show();
               return false;
            });
            $("#newdispatch").click(function(){
               if($("#g_dispatch_4").size() == 1)
               {
                  $tabs.tabs("select", 1);
                  return false;
               }
               {%if $dispatchTemplates|@count > 1%}
               if($askTemplate)
               {
                  $askTemplate.find("select").val("").end().dialog("open");
               } else
               {
                  $askTemplate = $("<div>" +
                        "<label>{%$TR->meccano->template|escape:'javascript'%}:</label><br /><br />" +
                        "<select name='useTemplate' id='useTemplate'>" +
                           "<option value=''> -- {%$TR->meccano->dispatchtemplate|escape:'javascript'%}? -- </option>" +
                           {%foreach from=$dispatchTemplates item='v' name='i'%}
                           "<option value='{%$v.id%}'>{%$v.title%}</option>" +
                           {%/foreach%}
                        "</select>" +
                  "</div>").dialog({
                     modal: true, 
                     title: '{%$TR->meccano->selecttemplate|escape:"javascript"%}',
                     buttons: {
                        '{%$TR->meccano->dispatchnew|escape:"javascript"%}': function(){
                           var v = $("select", this).val();
                           if(v)
                           {
                              $tabs.tabs("add", "/admin/dispatch/new?template="+v, "{%$TR->meccano->dispatchnew%}", 1);
                              $(this).dialog("close");
                           } else 
                           {
                              meccano.notification_failure('{%$TR->notification->tpl_not_selected|escape:"javascript"%}');
                           }
                        },
                        'Cancel': function()
                        {
                           $(this).dialog("close");
                        }
                     }
                  });
               }
               {%else%}
               {%assign var='dTpl' value=$dispatchTemplates|@reset%}
               $tabs.tabs("add", "/admin/dispatch/new?template={%$dTpl.id%}", "{%$TR->meccano->dispatchnew%}", 1);
               {%/if%}
            });
            $("#addDispatchCategory").click(function(){
               $("#dispatchCategoriesList").append(
                  "<li class='fieldset'>" +
                     "<label>{%$TR->datatypes->category|escape:'javascript'%} (ID, Alias):</label>" +
                     "<input type='text' name='f[dispatch_category][]' value='' />" +
                     "<div class='fl-right'><a class='a_icon i_delete'>{%$TR->meccano->delete|escape:'javascript'%}</a></div>" +
                     "<div class='clear'></div>" +
                  "</li>"
               );
               return false;
            });
            $("#dispatchCategoriesList").delegate("a.i_delete", "click", function(){
               $(this).parent().parent().remove();
               return false;
            });
            $("#t_dispatch_templ").delegate('input.cancel', 'click', function(){
               $("#templateArea").parent().hide();
            });
            $("#dispatchTemplatesList").delegate('.tmpl-edit', 'click', function(){
               var tmplid = $(this).data('id');
               $.get('/admin/dispatch.php?a=3&id='+tmplid, function(r){
                  if(r.result)
                  {
                     $("#tmpl_id").val(r.data.id);
                     $("#tmpl_title").val(r.data.title);
                     $("#tmpl_from_name").val(r.data.from_name);
                     $("#tmpl_from_email").val(r.data.from_email);
                     $("#tmpl_description").val(r.data.description);
                     $("#tmpl_template").val(r.data.template);
                     $("#templateArea").parent().show();
                  }
               }, 'json');
               return false;
            });
            $("#dispatchTemplatesList").delegate('.tmpl-delete', 'click', function(){
               var tmplid = $(this).data('id');
               $.get('/admin/dispatch.php?a=2&id='+tmplid, afterTemplSaved, 'json');
               return false;
            });

            // TEMPLATES COMPILATION
            tpl_templates = Handlebars.compile($("#templates-list").html());
         });

         function afterTemplSaved(r)
         {
            $("#templateArea").parent().hide();
            meccano.notification_success('{%$TR->notification->tpl_saved|escape:"javascript"%}');
            $("#dispatchTemplatesList").html(tpl_templates(r));
         }
      </script>
      
      <style type="text/css">
         ul.categoriesList {
            padding: 0 0 0 15px;
            margin: 0;
            list-style: none;
            width: 400px;
         }
         ul.categoriesList li {
            list-style: none;
         }
      </style>
   </head>
   <body>
      <div id="tabedContent">

         <ul>
            <li><a href="#t_dispatch_list"><span>{%$TR->meccano->dispatches%}</span></a></li>
            {%if $authorizedUser->id eq 1%}
            <li><a href="#t_dispatch_templ"><span>{%$TR->meccano->dispatchtemplate%}</span></a></li>
            <li><a href="#t_dispatch_settings"><span>{%$TR->meccano->dispatchsettings%}</span></a></li>
            {%/if%}
         </ul>


         <div id="t_dispatch_list">
            {%if $dispatchList%}
            <table class="grid">
               <tr>
                  <th>{%$TR->meccano->date%}</th>
                  <th>{%$TR->meccano->user%}</th>
                  <th>{%$TR->meccano->subject%}</th>
                  <th>{%$TR->meccano->groups%}</th>
                  <th>{%$TR->subscribe->c_recipients%}</th>
               </tr>
               
               {%foreach from=$dispatchList item='v' name='i'%}
               <tr>
                  <td>{%$v.sdate%}</td>
                  <td>{%$v.user%}</td>
                  <td>{%$v.subject%}</td>
                  <td>
                     {%",<br />"|@join:$v.groups%}
                  </td>
                  <td>{%$v.emails%}</td>
               </tr>
               {%/foreach%}
            </table>
            {%/if%}
            <br /><br />
            <input type="button" class="btn" value=" {%$TR->meccano->dispatchnew%} " id="newdispatch" />
         </div>

         {%if $authorizedUser->id eq 1%}
         <div id="t_dispatch_templ">

            <div id="dispatchTemplatesList">
            {%if !empty($dispatchTemplates)%}
            <table width="99%" class="grid" cellpadding="0" cellspacing="1">
               <tr>
                  <th>{%$TR->meccano->title%}</th>
                  <th>{%$TR->meccano->description%}</th>
                  <th></th>
               </tr>
               {%foreach from=$dispatchTemplates item='v' name='i'%}
               <tr>
                  <td>{%$v.title%}</td>
                  <td class="notice">{%$v.description%}</td>
                  <td class="ct">
                     <a href="#" class="tmpl-edit" data-id="{%$v.id%}"><img width="16" height="16" src="/tpa/img/pencil.gif"></a>
                     <a href="#" class="tmpl-delete" data-id="{%$v.id%}"><img width="16" height="16" src="/tpa/img/16/delete.gif"></a>
                  </td>
               </tr>
               {%/foreach%}
            </table>
            {%/if%}
            </div>

            <input type="button" class="btn" value=" {%$TR->meccano->newdispatchtemplate%} " id="newdispatchtemplate" />
            <div style="margin: 20px 0; display: none;">
               <form id="templateArea">
                  <div class="fl-right" style="border: 1px solid #CCC; padding: 10px 20px;width: 400px;">
                     <b>%username%</b> - {%$TR->subscribe->subscriber_name%}<br />
                     <b>%article% ... %/article%</b> - {%$TR->subscribe->article_item%}<br />
                     <b>%unsubscribe% ... %/unsubscribe%</b> - {%$TR->subscribe->unsubscribe_link%}<br />
                  </div>
                  <input type="hidden" id="tmpl_id" name="id" value="" />
                  
                  <label for="tmpl_title">{%$TR->meccano->title%}:</label><br />
                  <input type="text" name="f[title]" id="tmpl_title" /><br /><br />
                  
                  <label for="tmpl_from_name">{%$TR->subscribe->from_name%}</label><br />
                  <input type="text" name="f[from_name]" id="tmpl_from_name" /><br /><br />
                  
                  <label for="tmpl_from_email">{%$TR->subscribe->from_email%}</label><br />
                  <input type="text" name="f[from_email]" id="tmpl_from_email" /><br /><br />
                  
                  <label for="tmpl_description">{%$TR->meccano->description%}:</label><br />
                  <textarea style="width: 40%; height: 60px;" name="f[description]" id="tmpl_description"></textarea><br /><br />
                  
                  <label for="tmpl_template">{%$TR->meccano->template%}:</label><br />
                  <textarea style="width: 90%; height: 400px;" name="f[template]" id="tmpl_template"></textarea><br /><br />
                  
                  <input type="submit" value=" {%$TR->meccano->save%} " />
                  <input type="button" class="cancel" value=" {%$TR->meccano->cancel%}" />
               </form>
            </div>
         </div>

         <div id="t_dispatch_settings">
            <form method="post" action="/admin/dispatch/settings">
            <div class="fl-left">{%$TR->datatypes->category%}:</div>
            <ul class="fl-left categoriesList" id="dispatchCategoriesList">
               {%if $dispatchSettings && $dispatchSettings.categories%}
               {%foreach from=$dispatchSettings.categories item='c'%}
               <li class="fieldset">
                  <img width="16" height="16" style="margin-right: 15px;" src="/tpa/img/16/document_arrow.png">
                  {%$categories[$c]->title%}
                  <input type="hidden" name="f[dispatch_category][]" value="{%$c%}" />
                  <div class="fl-right"><a class="a_icon i_delete" onclick="categoriesSearch.del(this);" href="javascript:void(0)">Удалить</a></div>
                  <div class="clear"></div>
               </li>
               {%/foreach%}
               {%/if%}
            </ul>
            <div class="clear"></div>
            <a href="javascript:void(0)" id="addDispatchCategory" class="a_icon i_add">{%$TR->meccano->addcategory%}</a>
            <br /><br />
            <input type="submit" class="btn" value=" {%$TR->meccano->save%} " />
            </form>
         </div>
         {%/if%}

      </div>

      <script id="templates-list" type="text/x-handlebars-template">
         <table width="99%" class="grid" cellpadding="0" cellspacing="1">
            <tr>
               <th>{%$TR->meccano->title%}</th>
               <th>{%$TR->meccano->description%}</th>
               <th></th>
            </tr>
            {{#each data}}{{#with this}}
            <tr>
               <td>{{title}}</td>
               <td class="notice">{{description}}</td>
               <td class="ct">
                  <a href="#" class="tmpl-edit" data-id="{{id}}"><img width="16" height="16" src="/tpa/img/pencil.gif"></a>
                  <a href="#" class="tmpl-delete" data-id="{{id}}"><img width="16" height="16" src="/tpa/img/16/delete.gif"></a>
               </td>
            </tr>
            {{/with}}{{/each}}
         </table>
      </script>
      
      <script id="templates-type-form" type="text/x-handlebars-templates">
         <label>{%$TR->meccano->template%}:</label><br />
         <select name="useTemplate" id="useTemplate">
            <option value=""> -- {%$TR->meccano->dispatchtemplate%}? -- </option>
            {{#each data}}{{#with this}}
            <option value="{{id}}">{{title}}</option>
            {{/with}}{{/each}}
         </select>
      </script>
   </body>
</html>
